﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Collections;
using System.Net.Mail;
using System.Text;

public partial class admin_admins_view_request : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MembershipUserCollection users = Membership.GetAllUsers();
        MembershipUserCollection uni_user = new MembershipUserCollection();
        ArrayList list = new ArrayList();

        foreach (MembershipUser user in users){
            string uni = user.Comment;
            Label label = (Label)FormView1.FindControl("UNIVERISTY_NAMELabel");
            string request_uni = label.Text;

            if (String.Compare(uni,request_uni,false ) == 0 )
            {
                uni_user.Add(user);
                list.Add(user.UserName);
            }
        }

        DropDownList1.DataSource = list;
        DropDownList1.DataBind();

        
            
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        MembershipUser user = Membership.GetUser(DropDownList1.SelectedValue.ToString());
        Label label = (Label)FormView1.FindControl("CONTACT_EMAILLabel");
        Label nlabel = (Label)FormView1.FindControl("CONTACT_NAMELabel");
        string email = label.Text;
        int id = int.Parse(FormView1.DataKey["ID"].ToString());

        StringBuilder str = new StringBuilder();
        str.AppendLine("<h1>Your ACS Login</h1><br/>");
        str.AppendLine("<p>Hello "+ nlabel.Text +", your admin login request has been accepted. Here are the login details for your ACS;</p>");
        str.AppendLine("<p>Username: <strong>"+ DropDownList1.SelectedValue.ToString() +"</strong></p>");
        str.AppendLine("<p>Password: <strong>"+ user.GetPassword() +"</strong></p>");
        str.AppendLine("<p></p>");
        str.AppendLine("<p>Thank You!</p>");

        String body = str.ToString();

        MailMessage message = new MailMessage(new MailAddress("no-reply@acs-membership.com", "ACS Membership"),new MailAddress(email));
        
        message.Subject = "Your ACS Login";
        message.Body = body;
        message.BodyEncoding = Encoding.ASCII;
        message.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient();
        smtp.Send(message);

        ACSService.updateRequestStatus(id, true, user.UserName);

        msgLabel.Text = "Login details has been sent to "+ email;

    }
}